package com.foreknow.demo9;

public class UserManager {
    String[] name = new String[2];
    String[] pass = new String[2];

    public void register(String username,String password) throws CustomException{
        // 查看字符串的长度
        int nameLength = username.length();
        int passLength = password.length();

        // 校验用户名和密码是否为空
        if (username.equals("") || password.equals("")){
            // 抛出异常
            throw new CustomException("用户名或密码为空！");
        }

        // 判断用户名nameLength的长度
        if (nameLength < 6 || nameLength > 14){
            throw new CustomException("用户名的长度不符合规范！");
        }

        // 判断密码passLength的长度
        if (passLength < 8 || passLength > 16){
            throw new CustomException("密码的长度不符合规范！");
        }

        for (int i = 0; i < name.length; i++) {
            if (name[i] == null && pass[i]== null){
                name[i] = username;
                pass[i] = password;
                System.out.println("注册成功！");
                return;
            }
        }

        throw new CustomException("用户已经达到上限！");
    }

    public static void main(String[] args) {
        UserManager manager = new UserManager();
        try {
            manager.register("zhangsan","12345678");
            manager.register("zhangsan","12345678");
            manager.register("zhangsan","12345678");
        } catch (CustomException e) {
            e.printStackTrace();
        }
    }
}
